package com.demo.project.dao;

import com.demo.annotation.Component;
import com.demo.project.entity.Account;
import com.demo.project.utils.StringUtils;
import lombok.Data;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Data
@Component
public class AccountDataServer {
    /**
     * 这个map映射 帐号密码，和用户id-uid
     */
    private Map<String,String> accountIds = new HashMap<>();
    /**
     * 这个map映射uid 和账户信息之间的关系
     */
    private Map<String, Account> accountMap= new HashMap<>();

    private Map<String,String> accountWithTeam = new HashMap<>();

    /**
     * 得到该数据持久化的一些信息。
     * @return
     */
    public List<PersistentConfig> getPersistenceConfigs(){
        List<PersistentConfig> configs = new ArrayList<>();
        //把三个属性map进行封装，并进行返回；
        PersistentConfig accountIdsConfig = new PersistentConfig(PersistenceUtil.basePath+ File.separator+"accountIds.txt",String.class,String.class,"accountIds");
        PersistentConfig accountMapConfig = new PersistentConfig(PersistenceUtil.basePath + File.separator + "accountMap.txt", String.class, Account.class,"accountMap");
        PersistentConfig accountWithTeamConfig = new PersistentConfig(PersistenceUtil.basePath + File.separator + "accountWithTeam.txt", String.class, String.class,"accountWithTeam");
        configs.add(accountIdsConfig);
        configs.add(accountMapConfig);
        configs.add(accountWithTeamConfig);
        return configs;
    }

    public Map<String, String> getAccountIds() {
        return accountIds;
    }

    public void setAccountIds(Map<String, String> accountIds) {
        this.accountIds = accountIds;
    }

    public void addTeam(String uid,String teamId){
        accountWithTeam.put(uid,teamId);
    }
    public void removeTeam(String uid,String teamId){
        accountWithTeam.remove(uid,teamId);
    }
    public String getTeamId(String uid){
        return accountWithTeam.get(uid);
    }


    public String getUidByAccountNumber(String accountNumber, String password){
        String accountPassword = StringUtils.getAccountPassword(accountNumber, password);
        return accountIds.get(accountPassword);
    }

    public Account getAccountInfoByUid(String uid){
        return accountMap.get(uid);
    }
    /**
     * 检查帐号密码是否重复
     * @param accountNumber
     * @param password
     * @return
     */

    public boolean isRepeatAccountNumber(String accountNumber,String password){
        String accountPassword = StringUtils.getAccountPassword(accountNumber, password);
        String accountId = accountIds.get(accountPassword);
        return (accountId==null)?false:true;
    }


    /**
     * 注册新用户
     * @param a 帐号
     * @param password 密码
     * @param account 账户信息
     * @return 返回uid
     */
    public String addAccount(String a,String password,Account account){
        String accountPassword = StringUtils.getAccountPassword(a, password);
        String uid = StringUtils.getRandomId();
        accountIds.put(accountPassword,uid);
        accountMap.put(uid,account);
        return uid;
    }

    /**
     * 根据账户密码 来删除 用户信息
     * @param account
     * @param password
     * @return
     */
    public boolean deleteAccount(String account,String password){
        String accountPassword = StringUtils.getAccountPassword(account, password);
        String uid = accountIds.get(accountPassword);
        if(uid==null){
            return false;
        }
        accountIds.remove(accountPassword);
        Account remove = accountMap.remove(uid);
        return (remove==null)?false:true;
    }


}
